using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Finish_Panel : Panel_Base
{
    public Image mask_Image;
    public Text title_Text;
    public Button backHome_Button;
    public Button next_Button;

    public Transform settlementInit_Point_0;
    public Transform settlementInit_Point_1;
    public AnimationCurve settlementItemSizeCurve;
    public float settlementTimeScale = 1;
    public RectTransform settlement_Item_Prefab;
    //public IEnumerator settlementIE;
    public IEPool_Manager.DeltaTimeData deltaTimeData=new IEPool_Manager.DeltaTimeData(0);
    private void Start()
    {
        settlementTimeScale = 1;
        backHome_Button.onClick.AddListener(() => {
            UIManager.instance.SwitchBlackMaskPerformance(true, 1, () => {
                UnityEngine.SceneManagement.SceneManager.LoadScene("Start");
            });
        });
        next_Button.onClick.AddListener(() => {
            GameManager.instance.ReStart(GameManager.instance.spawnBoxCount + 5);
        });
    }
    private void Update()
    {
        deltaTimeData.deltaTime = Time.deltaTime* settlementTimeScale;
    }
    protected override void OnEnable()
    {
        base.OnEnable();

        backHome_Button.gameObject.SetActive(false);
        next_Button.gameObject.SetActive(false);
        title_Text.color = new Color(1, 1, 1, 0);
        mask_Image.color = Color.clear;
        float time = 0.5f;
        IEPool_Manager.instance.KeepTimeToDo(null, time, null, (_time) => {
            mask_Image.color = new Color(0, 0, 0, Mathf.Lerp(0,0.8f, 1 - _time / time));
            title_Text.color =new Color(1, 1, 1, 1 - _time / time);
            return true;
        }, () =>
        {
            mask_Image.color = new Color(0, 0, 0, 0.8f);
            title_Text.color = Color.white;
            System.Action<List<Unit_Goods_Ctrl>, int> settlementAction = null;
            List<Unit_Goods_Ctrl> goods_List = GameManager.instance.car_Ctrl.floorGoods_List;
            if (goods_List.Count > 0)
            {
                float goodsShowTime = 0.5f;
                settlementAction = (_goods_List, _index) =>
                {
                    RectTransform settlement_Item = ObjectPool_Manager.instance.Create(settlement_Item_Prefab.GetInstanceID() + "", settlement_Item_Prefab, settlement_Item_Prefab.parent);
                    Image image = settlement_Item.GetChild<Image>();
                    foreach (Unit_Goods_Ctrl.GoodsType goodsType in goods_List[_index].GetGoodsType())
                    {
                        switch (goodsType)
                        {
                            case Unit_Goods_Ctrl.GoodsType.Deformable:
                                image.sprite = GameManager.instance.goodsTypeSprite_Group["Deformable"];
                                break;
                            case Unit_Goods_Ctrl.GoodsType.Fragile:
                                image.sprite = GameManager.instance.goodsTypeSprite_Group["Fragile"];
                                break;
                            case Unit_Goods_Ctrl.GoodsType.Up:
                                image.sprite = GameManager.instance.goodsTypeSprite_Group["Up"];
                                break;
                            default:
                                image.sprite = GameManager.instance.goodsTypeSprite_Group["Default"];
                                break;
                        }
                    }
                    image.color = goods_List[_index].isBroken ? Color.red : Color.white;
                    IEPool_Manager.instance.KeepTimeToDo(null, goodsShowTime, deltaTimeData, (_time) =>
                    {
                        settlement_Item.transform.position = Vector3.Lerp(settlementInit_Point_0.position, settlementInit_Point_1.position, (1 - _time / goodsShowTime) * 0.5f);
                        settlement_Item.transform.localScale = Vector3.Lerp(Vector3.zero, Vector3.one, settlementItemSizeCurve.Evaluate(1 - _time / goodsShowTime));
                        return true;
                    }, () =>
                    {
                        Vector3 initPos = settlement_Item.transform.position;
                        IEPool_Manager.instance.WaitTimeToDo(null, 0.5f, deltaTimeData, () =>
                        {
                            IEPool_Manager.instance.KeepTimeToDo(null, goodsShowTime, deltaTimeData, (_time) =>
                            {
                                settlement_Item.transform.position = Vector3.Lerp(initPos, settlementInit_Point_1.position, 1 - _time / goodsShowTime);
                                settlement_Item.transform.localScale = Vector3.Lerp(Vector3.one, Vector3.zero, settlementItemSizeCurve.Evaluate(1 - _time / goodsShowTime));
                                return true;
                            }, () =>
                            {
                                ObjectPool_Manager.instance.Hide(settlement_Item_Prefab.GetInstanceID() + "", settlement_Item.gameObject);
                            });
                            _index++;
                            if (_index < goods_List.Count)
                            {
                                settlementAction(_goods_List, _index);
                            }
                            else
                            {
                                backHome_Button.gameObject.SetActive(true);
                                IEPool_Manager.instance.WaitTimeToDo(null, 0.32f, null, () => {
                                    next_Button.gameObject.SetActive(true);
                                });
                            }
                        });
                    });

                };
                settlementAction(goods_List, 0);
            }
            else
            {
                backHome_Button.gameObject.SetActive(true);
                IEPool_Manager.instance.WaitTimeToDo(null, 0.32f, null, () => {
                    next_Button.gameObject.SetActive(true);
                });
            }
        });
    }
    public void SetSettlementSpeed(int speed)
    {
        settlementTimeScale = speed;
    }
}
